package com.zxy.paycenter.modules.alipay.controller;

import com.zxy.paycenter.common.aop.RequireSignature;
import com.zxy.paycenter.common.constants.Constant;
import com.zxy.paycenter.common.message.request.CommodityReq;
import com.zxy.paycenter.modules.BaseController;
import com.zxy.paycenter.modules.alipay.service.AlipayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Properties;

/**
 * @author zxy
 */
@Slf4j
@Controller
@Api(tags = "支付宝支付")
@RequestMapping(value = "/alipay")
public class AlipayController extends BaseController {

    @Resource
    private AlipayService aliPayService;

    @RequireSignature
    @ApiOperation(value = "电脑支付")
    @PostMapping(value = "/pcPay")
    public String pcPay(HttpServletRequest request, ModelMap modelMap, CommodityReq commodityReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:电脑支付", merchantName);
        String form = aliPayService.aliPayPc(commodityReq, properties);
        modelMap.addAttribute("form", form);
        log.info("响应信息:{}", form);
        return "alipay/pay";
    }

    @RequireSignature
    @ApiOperation(value = "手机H5支付")
    @PostMapping(value = "/mobilePay")
    public String mobilePay(HttpServletRequest request, ModelMap modelMap, CommodityReq commodityReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:手机H5支付", merchantName);
        String form = aliPayService.aliPayMobile(commodityReq, properties);
        modelMap.addAttribute("form", form);
        return "alipay/pay";
    }

    @RequireSignature
    @ApiOperation(value = "二维码支付")
    @PostMapping(value = "/qrCodePay")
    public String qrCodePay(HttpServletRequest request, ModelMap modelMap, CommodityReq commodityReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:二维码支付", merchantName);
        String message = aliPayService.aliQrCodePay(commodityReq, properties);
        if (Constant.SUCCESS.equals(message)) {
            String img = "../qrcode/" + commodityReq.getOutTradeNo() + ".png";
            modelMap.addAttribute("img", img);
        } else {
            log.info("获取二维码失败");
        }
        return "alipay/qcpay";
    }

    @RequireSignature
    @ApiOperation(value = "app支付服务端")
    @PostMapping(value = "/appServicePay")
    public String appServicePay(HttpServletRequest request, ModelMap modelMap, CommodityReq commodityReq) {
        Properties properties = (Properties) request.getAttribute("aliConfig");
        String merchantName = properties.getProperty("merchantName");
        log.info("{}:app支付服务端", merchantName);
        String orderString = aliPayService.appServicePay(commodityReq, properties);
        modelMap.addAttribute("orderString", orderString);
        return "alipay/pay";
    }
}
